package fyp.spedmew;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class CreateJobAddPictureActionBarActivity extends ActionBarActivity {

    static int RESULT_LOAD_IMAGE = 1;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    private String photoPath;
    private Uri selectedImage;

    Bundle jobData = new Bundle();
    /* public static final String key_name = "Name";
    public static final String key_description = "Item description";
    public static final String key_shipment_date = "shipment date";
    public static final String key_price = "price";
    public static final String Key_pic = "Pic url"; */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_job_add_picture);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageResource(R.drawable.no_image);
        Intent in = getIntent();
        jobData = in.getExtras();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_create__shipment2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);



        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

        }
        else if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            selectedImage = Uri.parse(photoPath);
        }

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageURI(selectedImage);
    }

    public void takePhoto(View view) {
        final Button btn = (Button) findViewById(R.id.photo_btn);
        btn.setBackgroundColor(getResources().getColor(R.color.button_BG_OnPressed));

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                btn.setBackgroundColor(getResources().getColor(R.color.button_BG));
            }
        }, 300);

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (i.resolveActivity(getPackageManager()) != null)
        {
            File imgFile = null;

            try
            {
                imgFile = createImgFile();
            }
            catch(IOException ex) {  }

            if(imgFile != null) {
                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
                startActivityForResult(i, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    private File createImgFile() throws IOException
    {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imgFilename = "JPEG" + timeStamp + "_";
        File storagePath = Environment.getExternalStorageDirectory();

        File img = File.createTempFile(imgFilename, ".jpg", storagePath);
        photoPath = img.toString();

        return img;
    }

    public void loadPicture(View view)
    {
        final Button btn = (Button) findViewById(R.id.loadPic_btn);
        btn.setBackgroundColor(getResources().getColor(R.color.button_BG_OnPressed));

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                btn.setBackgroundColor(getResources().getColor(R.color.button_BG));
            }
        }, 300);

        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }

    public void sendMessage(View view) {
        final Button btn = (Button) findViewById(R.id.nextPic_btn);
        btn.setBackgroundColor(getResources().getColor(R.color.button_BG_OnPressed));

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                btn.setBackgroundColor(getResources().getColor(R.color.button_BG));
            }
        }, 300);

        if (selectedImage == null){
            selectedImage = Uri.parse("android.resource://fyp.spedmew/drawable/no_image");
        }

        Intent out = new Intent(this, CreateJobGetLocationActionBarActivity.class);
        jobData.putString("pic", selectedImage.toString());
        out.putExtras(jobData);

        startActivity(out);
    }
}
